package com.scy.controller2;

import com.scy.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.Random;

@Controller
@RequestMapping("/my2")
public class MyController2 {


    @RequestMapping("/load")
    @ResponseBody
    public User testLoadPropertes() {
        User user = new User();

        System.out.println("-------------进入testLoadPropertes方法了--------------");

        try {
            Properties properties = new Properties();

            InputStream in = MyController2.class.getClassLoader().getResourceAsStream("conf/my.properties");
            InputStreamReader inputStreamReader = new InputStreamReader(in, "utf-8");
            properties.load(inputStreamReader);

            String name = properties.getProperty("name");
            String age = properties.getProperty("age");

            Random random = new Random();
            int i = random.nextInt(9) + 1;

            user.setName(name);
            user.setAddr(age);
            return user;

//            if (i > 5) {
//                user.setName(name);
//                user.setAddr(age);
//                return user;
//            } else {
//                throw new RuntimeException("----发生异常了---- i="+i);
//            }
        } catch (Exception e) {
            user.setName("name异常");
            user.setAddr("addr异常");
            e.printStackTrace();
        }

        return user;
    }

}
